<?php
//商品图片管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class PictureController extends CommonController
{
    
    public function index()
    {
      //
    }
	//商品图片添加操作界面
    public function create()
    {
		$mid = $_GET['mid'];
		if(is_numeric($mid)){
			$data = \DB::table("m_info")->get();
			return view('admin.pic.add',['list'=>$data,'mid'=>$mid]);
		}else{
			$data = \DB::table("m_genre")->get();
			return view('admin.pic.create',['list'=>$data,'name'=>$mid]);
		}
    }
	//商品图片后台添加
    public function store(Request $request)
    {
		$data = $request->all();
		$data = $request->except('_token');
		if(!is_numeric($data['mid'])){
			$pic = new \App\Org\Upload("pic");
			$pic->path="uploads/"; 
			$pic->typeList =array("image/jpeg","image/png","image/gif"); 
			$pic->maxSize =0; 
			$res = $pic->doUpload();
			$data['pic'] = $res;
			$id = \DB::table("m_pic")->insertGetId($data);
		}else{
			$n = \DB::table("m_pic")->where('mid',$data['mid'])->count();
			//判断		
			if($n<5){
				$pic = new \App\Org\Upload("pic");
				$pic->path="uploads/"; 
				$pic->typeList =array("image/jpeg","image/png","image/gif"); 
				$pic->maxSize =0; 
				$res = $pic->doUpload();
				$data['pic'] = $res;
				$id = \DB::table("m_pic")->insertGetId($data);
			}else{
				$info = "添加失败,图片数量达到上限";
				return view("admin.pic.info",['info'=>$info]);
			}
		}
		return redirect()->action('admin\PictureController@show',[$data['mid']]);
    }
	//单个商品图片管理浏览
    public function show($id)
    {		
		$data = \DB::table("m_pic")->where('mid',$id)->first();
		if(empty($data)){
			$mid = \DB::table("m_pic")->insertGetId(['mid' => $id]);
		}
		$data = \DB::table("m_pic")->where('mid',$id)->paginate(5);
		return view("admin.pic.show",['list'=>$data]);
    }
	//商品图片修改操作界面
    public function edit($id)
    {
		$data = \DB::table("m_pic")->where('id',$id)->first();
		return view("admin.pic.edit",['list'=>$data]);
    }
	//商品图片后台修改
    public function update(Request $request, $id)
    {
		$data =	$request->all();
		$data = $request->except('_token','_method');
		$pic = new \App\Org\Upload("pic");
		$pic->path="uploads/"; 
        $pic->typeList =array("image/jpeg","image/png","image/gif"); 
        $pic->maxSize =0; 
        $res = $pic->doUpload();
		$data['pic'] = $res;
		\DB::table('m_pic')->where('id',$id)->update($data);
		return redirect()->action('admin\PictureController@show',[$data['mid']]);
    }
	//商品图片后台删除
    public function destroy($id)
    {
		\DB::table('m_pic')->where('id', $id)->delete();
		 return back()->withInput();
		//$info = "商品图片删除成功！";
        //return view("admin.pic.info",['info'=>$info]);
    }
}
